package com.eugenes.design.cardvalidator;

import com.eugenes.collections.MyHashMap;
import com.eugenes.design.Card;

public class MultipleCountCardValidator implements CardValidator {
    MyHashMap<Card, Integer> cards = new MyHashMap<>();

    private void ifNullThrowIllegalArgException(Object obj) {
        if (obj == null) {
            throw new IllegalArgumentException("Argument is null.");
        }
    }

    @Override
    public boolean validate(Card card) {
        ifNullThrowIllegalArgException(card);
        Integer left = cards.get(card);
        return left != null && left > 0;
    }

    @Override
    public void cardIsUsed(Card card) {
        ifNullThrowIllegalArgException(card);
        Integer left = cards.get(card);
        if (left != null) {
            cards.put(card, left - 1);
        }
    }

    public int addCard(Card card, int counts) {
        Integer result = cards.get(card);
        if (result == null) {
            result = 0;
        }
        cards.put(card, counts);
        return result;
    }
}
